#include<Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS1302.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>//设置字体
#include <LowPower.h>
#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define SCREEN_ADDRESS 0x3C
#define KEY2 8
#define KEY1 9
#define LED 10
int flag_adj=0;//是否进入调整界面
int flag_off=0;//关闭屏幕
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);//OLED


const int kCePin   = 5;  // 复位引脚
const int kIoPin   = 6;  // 数据引脚
const int kSclkPin = 7;  // 时钟引脚

DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "SUN";
    case Time::kMonday: return "MON";
    case Time::kTuesday: return "TUE";
    case Time::kWednesday: return "WED";
    case Time::kThursday: return "THU";
    case Time::kFriday: return "FRI";
    case Time::kSaturday: return "SAT";
  }
  return "(unknown day)";
}
static const uint8_t PROGMEM LOGO[] ={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x08,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x10,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x00,0x00,0x00,0x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FF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x10,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x40,0x00,0x00,0x10,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,
};

void setup() {
   pinMode(KEY1,INPUT);
   pinMode(KEY2,INPUT);
   pinMode(LED,OUTPUT);
  
   Wire.begin();
   display.begin(SSD1306_SWITCHCAPVCC,SCREEN_ADDRESS);
   display.clearDisplay();
   display.setTextWrap(0);//不换行

   rtc.writeProtect(false);//去掉写保护
   rtc.halt(false);//清除停止标志
   Time t(2021,2, 1, 21, 22, 0, Time::kMonday);
   rtc.time(t);

   
}
int flag_LED=0;
void loop() {
    if(!flag_adj)
    {
      key_adj();
       //开机亮灯
       if(flag_LED)
      {
        digitalWrite(LED,HIGH);
        delay(2000);
        digitalWrite(LED,LOW);
        flag_LED=0;
      }
    }
    if(flag_adj)
    {
      //调整界面
      display.clearDisplay();
      adjust_time();
    }
    else if(flag_off)
    {
      display.clearDisplay();
      display.display();
      LowPower.powerDown(SLEEP_8S,ADC_OFF, BOD_OFF);
    }
    else
    { 
     get_time();   
     display_time();
     display.drawRoundRect(0,0,128,64,8,1);
     display_data();
     LowPower.powerDown(SLEEP_8S,ADC_OFF, BOD_OFF); 
    }
     display.display();
}
Time t = rtc.time();
const String day;

int flag_page2=0;
int flag_page3=0;
void adjust_time()
{
  rtc.halt(false);//停止走时
  digitalWrite(LED,HIGH);
  display.drawRoundRect(0,0,128,64,8,1);
  display.setTextColor(WHITE);
  display.setFont(&FreeSansBold9pt7b);
  int y=20;
  int x=13;
  static int item=1;
  if(digitalRead(KEY1))
  {
    delay(50);
    if(digitalRead(KEY1))
     {
      item++;
      if(item>4)
      item=1;
     }
  }
  
  display_adjust_arrow(item);
  if(!flag_page2)
  {
    adjust_page1(item);
    display_adjust_page_1(y,x);
  }
  else if(!flag_page3)
  {
    adjust_page2(item);
    display_adjust_page_2(y,x);
  }
  else
  {
     adjust_page3(item);
    display_adjust_page_3(y,x);
  }
}
void adjust_page1(int item)
{
  if(scan_key2())
  switch(item)
  {
    case 1:t.hr++;if(t.hr>23)t.hr=0;return;
    case 2:t.min++;if(t.min>59)t.min=0;return;
    case 3:t.sec++;if(t.sec>59)t.sec=0;return;
    case 4:flag_page2=1;return;
  }
}
int t_day=t.day;
void adjust_page2(int item)
{
  //arduino定义不同
  if(scan_key2())
  switch(item)
  {
    case 1:t_day++;if(t_day>7)t_day=1;adjust_t_day(t_day);return;
    case 2:t.mon++;if(t.mon>12)t.mon=1;return;
    case 3:t.date++;if(t.date>31)t.date=1;return;
    case 4:flag_page3=1;return;
  }
}
void adjust_page3(int item)
{
 
  if(scan_key2())
  switch(item)
  {
    case 1:t.yr++;if(t.yr>2099)t.yr=2000;return;
    case 2:display_logo();quit_dis();return;
    case 4:quit_dis();return;
  }
}

void display_logo()
{
  display.clearDisplay();  
  display.drawBitmap(0,0,LOGO,128,64,WHITE);
  display.display();
  delay(10000);
  return;
}
void quit_dis()
{
  flag_adj=0;
  flag_page2=0;
  flag_page3=0;
  rtc.time(t);
  digitalWrite(LED,LOW);
}
void adjust_t_day(int t_day)
{
  switch(t_day)
  {
    case 1:t.day=2;return;
    case 2:t.day=3;return;
    case 3:t.day=4;return;
    case 4:t.day=5;return;
    case 5:t.day=6;return;
    case 6:t.day=7;return;
    case 7:t.day=1;return;
  }
}


int scan_key2()
{
  if(digitalRead(KEY2))
  {
    delay(50);
    if(digitalRead(KEY2))
    return 1;
  }
  return 0;
}
void display_adjust_arrow(int x)
{
  int x_adj;
  x_adj=12+16*(x-1);
  display.setCursor(5,x_adj);
  display.print(">");
}
void display_adjust_page_1(int y_adj,int x_adj)//item表示正在处理的内容
{
  display.setCursor(y_adj,x_adj);
  display.print("hour  ");
  display.setCursor(y_adj+78,x_adj);
  display.print(t.hr);
  display.setCursor(y_adj,x_adj+16);
  display.print("minute  ");
  display.setCursor(y_adj+78,x_adj+16);
  display.print(t.min);
  display.setCursor(y_adj,x_adj+32);
  display.print("second  ");
  display.setCursor(y_adj+78,x_adj+32);
  display.print(t.sec);
  display.setCursor(y_adj,x_adj+48);
  display.print("next");

  //处理item到响应的调整阶段
  //读取现在的时间 并更新时间
  //长按进入键，将调整后时间更新到时钟模块中
}
void display_adjust_page_2(int y_adj,int x_adj)
{
  display.setCursor(y_adj,x_adj);
  display.print("day");
  display.setCursor(y_adj+78,x_adj);
  display.print(t_day);
 
  display.setCursor(y_adj,x_adj+16);
  display.print("month");
  display.setCursor(y_adj+78,x_adj+16);
  display.print(t.mon);
  display.setCursor(y_adj,x_adj+32);
  display.print("data");
  display.setCursor(y_adj+78,x_adj+32);
  display.print(t.date);
  display.setCursor(y_adj,x_adj+48);
  display.print("next");
}

void display_adjust_page_3(int y_adj,int x_adj)
{
  display.setCursor(y_adj,x_adj);
  display.print("year");
  display.setCursor(y_adj+78,x_adj);
  display.print(t.yr-2000);
 
  display.setCursor(y_adj,x_adj+16);
  display.print("logo");
  //display.setCursor(y_adj+78,x_adj+16);
 // display.print(t.mon);
  /*display.setCursor(y_adj,x_adj+32);
  display.print("data");
  display.setCursor(y_adj+78,x_adj+32);
  display.print(t.date);*/
  display.setCursor(y_adj,x_adj+48);
  display.print("quit");
}
void key_adj()
{
  if(digitalRead(KEY1)||digitalRead(KEY2))
  {
    delay(1000);
    if(digitalRead(KEY1))
     {
      flag_adj=1;//长按KEY1进入调整界面
     }
     if(digitalRead(KEY2))
     {
      flag_off=!flag_off;//长按KEY2关闭屏幕
      flag_LED=1;
     }
  }
}

void get_time()
{ 
  t = rtc.time();
  day =dayAsString(t.day);
}

void display_data()//显示日期
{
  display.setTextColor(BLACK);
  display.setFont(&FreeSansBold9pt7b);

      display.fillRoundRect(4,4,120,22,7,1);
      display.setCursor(10,21);//行号在后
      display.print(day);
      if(t.mon>=10&&t.date>=10)
        display.setCursor(51,21);
      else if(t.mon>=10||t.date>=10)
        display.setCursor(57,21);
      else 
        display.setCursor(64,21);
      display.print(t.yr-2000);
      display.print("/");
      display.print(t.mon);
      display.print("/");
      display.print(t.date);
      
}

/*#define dis_high 56
#define hour_cursor 7
#define minute_cursor 61
#define second_cursor 101*/
//定义·显示时分秒的列数

/*void display_time()
{
   //设置颜色
  display.setTextColor(WHITE);
  display.clearDisplay();
  //hour
  display.setFont(&FreeSansBold18pt7b);
  if(t.hr<10)
    {
      display.setCursor(hour_cursor,dis_high);
      display.print("0");
      display.print(t.hr);
    }
    else
    {
      display.setCursor(hour_cursor,dis_high);
      display.print(t.hr);
    }
  
  //minute
  if(t.min<10)
    {
      display.setCursor(minute_cursor,dis_high);
      display.print("0");
      display.print(t.min);
    }
    else
    {
      display.setCursor(minute_cursor,dis_high);
      display.print(t.min);
    }
    
  //second
  display.setFont(&FreeSansBold9pt7b);

  if(t.sec<10)
    {
      display.setCursor(second_cursor,dis_high);
      display.print("0");
      display.print(t.sec);
    }
    else
    {
      display.setCursor(second_cursor,dis_high);
      display.print(t.sec);
    }

     if(t.sec%2==0)//闪烁的点
    { 
      display.setFont(&FreeSansBold18pt7b);
      display.setCursor(minute_cursor-12,dis_high-2);
      display.print(":");
    }
}*/

#define dis_high 56
#define hour_cursor 5
#define minute_cursor 57
#define second_cursor 97
void display_time()//不显示秒
{
   //设置颜色
  display.setTextColor(WHITE);
  display.clearDisplay();
  //hour
  display.setFont(&FreeSansBold18pt7b);
  int t_hr=t.hr-12;
  if(t_hr<10)
    {
      display.setCursor(hour_cursor,dis_high);
      display.print("0");
      display.print(t_hr);
    }
    else
    {
      display.setCursor(hour_cursor,dis_high);
      display.print(t_hr);
    }
  
  //minute
  if(t.min<10)
    {
      display.setCursor(minute_cursor,dis_high);
      display.print("0");
      display.print(t.min);
    }
    else
    {
      display.setCursor(minute_cursor,dis_high);
      display.print(t.min);
    }
    
     display.setCursor(minute_cursor-12,dis_high-2);
     display.print(":");

       //second
  display.setFont(&FreeSansBold9pt7b);

  //上午或下午
  if(t.hr>12)
    {
      display.setCursor(second_cursor,dis_high);
      display.print("pm");
    }
    else
    {
      display.setCursor(second_cursor,dis_high);
      display.print("am");
    }

}
